import click.*
import grails.util.Environment
 

class BootStrap {
    def springSecurityService

    def init = { servletContext ->
  
        switch (Environment.current) {
            case Environment.DEVELOPMENT:
                System.out = new  PrintStream(System.out, true, "gbk") 
                break
            case Environment.PRODUCTION:
                System.out = new  PrintStream(System.out, true, "utf-8") 
                break
        }

        def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true) 
        def userRole = new Role(authority: 'ROLE_USER').save(flush: true)
        
        String password = springSecurityService.encodePassword('1qazxsw2') 
        def testUser = new User(username: 'admin', enabled: true, password: password) 
        testUser.save(flush: true)

        UserRole.create testUser, adminRole, true

        assert User.count() == 1 
        assert Role.count() == 2 
        assert UserRole.count() == 1 

    }
    def destroy = {
    }
}
